package zz.extjs.dbc;

import java.sql.Connection;
import java.sql.DriverManager;

public class DataBaseConnection {

	/**
	 * 连接access
	 * 
	 * private final String DBDRIVER="sun.jdbc.odbc.JdbcOdbcDriver"; private
	 * final String DBURL="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=E:\\Personal\\Administrator\\workspace-my\\my-ext-frame\\data\\extjsframe.mdb"
	 * ; private Connection conn=null;
	 */

	/**
	 * 连接数据库，用xml文件确定连接哪个库
	 * 连接同时设置日期格式 alter session set nls_date_format='YYYY"-"MM"-"DD'
	 */
	private String DBDRIVER = "org.apache.derby.jdbc.ClientDriver";
	private String DBURL = "jdbc:derby://localhost:1527/orcl";
	private Connection conn = null;

	public DataBaseConnection() {
		/**
		 * 用配置文件设置数据连接，文件webroot/classes/dbconection.xml
		 */
		ParseXML pxml = new ParseXML();
		try {
			pxml.parse("dbconection.xml");
			DBURL = (String)pxml.getProps().get("dburl");
			String user = (String)pxml.getProps().get("user");
			String pass = (String)pxml.getProps().get("pass");
			DBDRIVER = (String)pxml.getProps().get("driver_class");
			Class.forName(DBDRIVER);
			this.conn = DriverManager.getConnection(DBURL, user, pass);
			setDateFormat();	//设置日期格式
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void setDateFormat(){
		if(getDBDRIVER().indexOf("derby")>=0)
			return;
		try {
			this.conn.createStatement().executeUpdate("alter session set nls_date_format='YYYY\"-\"MM\"-\"DD'");
			
		} catch (Exception e) {
			return;
		}
	}
	public Connection getConnection() {
		return this.conn;
	}

	public void setConnection(Connection conn) {
		//设置改变数据库连接，主要为方便调试
		this.conn = conn;
	}

	public String getDBDRIVER() {
		return DBDRIVER;
	}

	public void close() {
		try {
			this.conn.close();
		} catch (Exception e) {
		}
	}
}
